<?php
namespace app\applet\model;

use app\common\model\User as UserModel;

/**
 * 用户模型
 */
class User extends UserModel
{
    /**
     * 关注公众号
     */
    public function subscribe($data,$applet_id=0)
    {
        //筛选条件
        $filter = [
            'open_id' => $data['open_id'],
            'applet_id' => $applet_id
        ];
        $password = rand(100000,999999);
        $user_name = time();
        $data['password'] = hema_hash($password); 
        if(!$model = User::where($filter)->where('status','>',10)->find()){
            $model = $this;
            $data['user_name'] = $user_name;
            $data['status'] = 20;
            $data['applet_id'] = $applet_id;
        }else{
            if(empty($model['user_name'])){
               $data['user_name'] = $user_name;
            }else{
                $user_name = $model['user_name'];
            }
        }
        $model->save($data);
        return [
            'password' => $password,
            'user_name' => $user_name
        ];
    }

    /**
     * 找回账号
     */
    public function retrieve()
    {
        return $this->save(['password' => hema_hash('123456')]) !== false;
    }

    /**
     * 公众号取消关注
     */
    public function unSubscribe()
    {
        return $this->save(['is_subscribe' => 0]) !== false;
    }
}
